<script setup lang="ts">
import { ref } from 'vue'
import { COLUMNS } from '../constants'

// 鼠标悬停
const hover = ref(true)

// 接收父组件传递的数据
const props = defineProps({
    data: {
        type: Object,
        default: () => ({})
    },
    total: Number,
    pagination: {
        type: Object,
        default: () => ({})
    }
})

// 回调给父组件的方法
const emits = defineEmits(['onChange', 'handleBulid', 'handleEdit', 'handleDelete', 'handleStatus'])

// 分页变动触发
const onChange = (pageInfo) => {
    emits('onChange', pageInfo)
}

const handleBulid = () => {
    emits('handleBulid')
}

const handleEdit = (row) => {
    if(!row.count) {
        emits('handleEdit', row)
    }
}

const handleDelete = (row) => {
    if (!row.count) {
        emits('handleDelete', row)
    }
}

const handleStatus = (row) => {
    if (!row.count) {
        emits('handleStatus', row)
    }
}


// 判断当前数据是否包含小数点
const isDecimals = (val) => {
    if (String(val).indexOf('.') > -1) {
        return true
    }
    return false
}
</script>

<template>
    <div class="baseList">

        <div class="newBox">
            <button class="bt wt-120" @click="handleBulid">
                新增护理项目
            </button>
        </div>

        <div class="tableBoxs">
            <t-table row-key="index" :data="data" :columns="COLUMNS" :hover="hover">
                <!-- 序号处理 -->
                <template #rowIndex="{ rowIndex }">{{ rowIndex + 1 }}</template>

                <!-- 是否包含小数点 -->
                <template #price="{ row }">{{
                    isDecimals(row.price) ? row.price : row.price + '.00'
                }}</template>

                <!-- 图片处理 -->
                <template #image="{ row }">
                    <div class="tdesign-demo-image-viewer__base">
                        <t-image-viewer :images="[row.image]">
                            <template #trigger="{ open }">
                                <div class="tdesign-demo-image-viewer__ui-image">
                                    <img alt="test" :src="row.image" class="tdesign-demo-image-viewer__ui-image--img" />
                                    <div class="tdesign-demo-image-viewer__ui-image--hover" @click="open">
                                        <span>
                                            <BrowseIcon size="1.4em" /> 预览
                                        </span>
                                    </div>
                                </div>
                            </template>
                        </t-image-viewer>
                    </div>
                </template>

                <!-- 按钮展示 -->
                <template #op="{ row }">
                    <div class="operateCon">
                        <a class="btn-dl" :class="row.count > 0 ? 'disabled' : ''" @click="handleDelete(row)">删除</a>
                        <a class="font-bt" :class="row.count > 0 ? 'disabled' : ''" @click="handleEdit(row)">编辑</a>
                        <a class="delete" :class="row.count > 0 && row.status === 1
                            ? 'disabled btn-dl'
                            : row.status === 1
                                ? 'btn-dl'
                                : 'font-bt'
                            " @click="handleStatus(row)"> {{ row.status === 1 ? '禁用' : '启用' }}</a>
                    </div>
                </template>
            </t-table>

            <!-- 分页组件 -->
            <t-pagination v-if="total > 10" v-model="pagination.pageNum" v-model:pageSize="pagination.pageSize"
                :total="total" @change="onChange" />
        </div>
    </div>
</template>
